package cn.edu.scau.dropship.controller;

import cn.edu.scau.dropship.entity.Brand;
import cn.edu.scau.dropship.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;


    @PostMapping(value = "/addBrand")
    @ResponseBody
    public String addBrand(String name, Integer mvo_id) {
        Brand brand = new Brand();
        brand.setName(name);
        brand.setMvoId(mvo_id);
        int result = brandService.insert(brand);
        if (result > 0) return "添加品牌成功";
        return "添加品牌失败";
    }

    @PostMapping(value = "/addBrandWithPic")
    @ResponseBody
    public String addBrand(String name, Integer mvo_id, String image) {
        Brand brand = new Brand();
        brand.setName(name);
        brand.setMvoId(mvo_id);
        brand.setImage(image);
        int re = brandService.insert(brand);
        if (re == 1) {
            return "添加品牌成功";
        }
        return "添加品牌失败";
    }


    @PostMapping("/getAllBrand")
    @ResponseBody
    public List<Brand> getAllBrand(Integer mvo_id) {
        return brandService.selectAllByMid(mvo_id);
    }


    @PostMapping("/updateBrand")
    @ResponseBody
    public String updateBrand(Integer id, String name, Integer mvo_id) {
        Brand brand = new Brand(id, name, mvo_id,null);
        int result = brandService.updateByPrimaryKey(brand);
        if (result > 0) {
            return "更新品牌讯息成功";
        }
        return "更新品牌讯息失败";
    }


    @PostMapping("/updateBrandWithPic")
    @ResponseBody
    public String updateBrand(Integer id, String name, Integer mvo_id, String image) {
        Brand brand = new Brand(id, name, mvo_id, image);
        int re = brandService.updateByPrimaryKey(brand);
        if (re > 0) {
            //System.out.println(brand.getId() + " " + brand.getName() + " " + brand.getMvoId() + ' ' + brand.getImage());
            return "更新品牌讯息成功";
        }
        return "更新品牌讯息失败";
    }


    @PostMapping("/deleteBrand")
    @ResponseBody
    public String deleteBrand(Integer id) {
        int re = brandService.deleteByPrimaryKey(id);
        if (re == 1) return "删除品牌成功";
        return "删除品牌失败";
    }


    @PostMapping("/selectBrandByUserID")
    @ResponseBody
    public List<Brand> selectBrandByUserID(Integer mvo_id) {
        List<Brand> list = brandService.selectAllByMid(mvo_id);
        return list;
    }


    @PostMapping("/deleteByName")
    @ResponseBody
    public String deleteByName(String name) {
        int re = brandService.deleteByName(name);
        if (re == 1) return "成功删除品牌" + name;
        return "删除失败";
    }


    @PostMapping("/selectBrandByName")
    @ResponseBody
    public Brand selectBrandByName(String name) {
        return brandService.selectByName(name);
    }


    @PostMapping("/insertWithPic")
    @ResponseBody
    public String insertWithPic(String name, Integer mvo_id, String image) {
        Brand brand = new Brand(null,name, mvo_id, image);
        int re = brandService.insert(brand);
        if (re == 1) {
            return "插入品牌訊息成功";
        }
        return "插入品牌訊息失敗";
    }


    @PostMapping("/selectBrandById")
    @ResponseBody
    public Brand selectByPrimaryKey(Integer id) {
        return brandService.selectByPrimaryKey(id);
    }

}
